package com.dkm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.SignInLog;
import com.dkm.service.SignInLogService;
import com.dkm.util.LayuiJsonUilt;

@RestController
@RequestMapping("/signInLog")
public class SignInLogController {

	//注入签到业务层
	@Autowired
	private SignInLogService signInLogService;
	
	@GetMapping("/getSignInLogByUser_id")
	public SignInLog getSignInLogByUser_id(String current_user_id) {
		SimpleDateFormat timestampsdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat datesdf=new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		Date date=new Date();
		//获取该用户今天的签到信息
		String d=datesdf.format(date);
		try {
			return signInLogService.getSignInLogByUser_idAndDate(current_user_id,timestampsdf.parse(d+" 00:00:00") ,timestampsdf.parse(d+" 23:59:59"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	@GetMapping
	@RequiresPermissions(value = "signInLog:query")
	public LayuiJsonUilt<SignInLog> findAllSignInLogs(SignInLog signInLog,LayuiJsonUilt<SignInLog> lj,String beginDate,String endDate) {
		Map<String, Object> map=new HashMap<String, Object>();
		
		if(signInLog!=null) {
			map.put("signInLog", signInLog);
		}
		if(beginDate!=null&&beginDate!="") {
			map.put("beginDate", beginDate);
		}
		if(endDate!=null&&endDate!="") {
			map.put("endDate", endDate);
		}
		Direction direction=Sort.Direction.DESC;
		Sort sort=JpaSort.unsafe(direction, "\\Qsign_in_time\\E");
		//创建分页对象//Sort.by(Direction.DESC,"sign_in_time")
		Pageable pageable=PageRequest.of(lj.getPage(), lj.getLimit(),sort);
		Page<SignInLog> page=signInLogService.findAllSignInLogs(map, pageable);
		
		//设置数据和长度
		lj.setData(page.getContent());
		lj.setCount(page.getTotalElements());
		
		return lj;
	}
}
